package com.wn.class30.service.impl;

import com.wn.class30.entity.PayOrder;
import com.wn.class30.entity.PayOrderExample;
import com.wn.class30.mapper.PayOrderDao;
import com.wn.class30.service.PayorderShareService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PayorderShareServiceImpl implements PayorderShareService {
    @Resource
    private PayOrderDao payOrderDao;
    @Override
    public List<PayOrder> selectPayorderShares(Map<String, String> map) throws Exception {
        return payOrderDao.selectByExample(getPayorderShareExample(map));
    }

    @Override
    public long count(Map<String, String> map) throws Exception {
        return payOrderDao.countByExample(getPayorderShareExample(map));
    }

    @Override
    public long selectCount(Integer abusinessId) throws Exception {
        PayOrderExample payOrderExample = new PayOrderExample();
        PayOrderExample.Criteria criteria = payOrderExample.createCriteria();
        criteria.andBussinessIdEqualTo(abusinessId);
        criteria.andPayOrderStateEqualTo("1");
        return payOrderDao.countByExample(payOrderExample);
    }
    public PayOrderExample getPayorderShareExample(Map<String,String> map) throws ParseException {
        PayOrderExample payOrderExample = new PayOrderExample();
        PayOrderExample.Criteria criteria = payOrderExample.createCriteria();
        Long page = Long.parseLong(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));
        String businessName = map.get("businessName");
        String businessId= map.get("businessId");
        String isSettlement= map.get("isSettlement");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        if(!"".equals(businessName) && businessName != null){
            criteria.andBusinessNameLike("%"+businessName+"%");

        }
        if(!"".equals(businessId) && businessId != null){

            criteria.andBussinessIdEqualTo(Integer.parseInt(businessId));

        }
        if(!"".equals(startDate) && startDate != null){

            criteria.andPaySharetimeGreaterThanOrEqualTo(simpleDateFormat.parse(startDate));
        }

        if(!"".equals(endDate) && endDate != null){
            Date date = simpleDateFormat.parse(endDate);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, 1);
            Date time = c.getTime();
            String format = simpleDateFormat.format(time);
            criteria.andPaySharetimeLessThanOrEqualTo(simpleDateFormat.parse(format));
        }
        if(!"".equals(isSettlement) && isSettlement != null){
            criteria.andPayOrderSharestatusEqualTo(Integer.parseInt(isSettlement));
        }
        criteria.andPayOrderStateEqualTo("1");
        payOrderExample.setLimit(limit);
        payOrderExample.setOffset(page);
        payOrderExample.setOrderByClause("`pay_order_sharestatus` ASC");
        return payOrderExample;
    }
}
